package cn.gorit.advice;

import cn.gorit.common.enums.ResultEnums;
import cn.gorit.common.lang.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLSyntaxErrorException;

/**
 * @version v1.0
 * @Classname GlobalSqlExceptionAdvice
 * @Description TODO
 * @Author CodingGorit
 * @Created on 2021/4/1 23:32
 * @IDLE IntelliJ IDEA
 **/
@RestControllerAdvice
public class GlobalSqlExceptionAdvice {

    private Logger log = LoggerFactory.getLogger(GlobalSqlExceptionAdvice.class);

    @ExceptionHandler(SQLSyntaxErrorException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result<Object> handleSQLSyntaxErrorException(SQLSyntaxErrorException ex) {
        log.error("【数据库名不对]: {}",ex.getMessage());
        return new Result<>(ResultEnums.SQL_ERROR.getCode(),ex.getMessage() + "数据库不存在");
    }
}
